package com.ai.business;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileFilter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class mp3 extends JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private BorderLayout bL = new BorderLayout();
    private JPanel Panel1 = new JPanel();
    private JPanel Panel2 = new JPanel();
    private JMenuBar menu = new JMenuBar();
    private JMenu Submenu = new JMenu();
    private JMenuItem SubmenuExit = new JMenuItem();
    private JTextArea listado = new JTextArea();
    private JButton buscar = new JButton();
    private JButton limpiar = new JButton();
    private JButton guardar = new JButton();
    private JButton browse = new JButton();
    private JFileChooser fc = new JFileChooser();
    private JLabel label = new JLabel();
    private JLabel cuantas = new JLabel();
    private File file;
    private File[] mp3s;
    private Calendar cd;
    private JScrollPane jScrollPane1 = new JScrollPane();
    int contador;
    JFileChooser chooser = new JFileChooser();

    public mp3() {
        super();
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        cd = Calendar.getInstance();
        this.getContentPane().setLayout(bL);
        Submenu.setText("Salir");
        SubmenuExit.setText("Chao!");
        listado.setText("");
        buscar.setText("Buscar");
        limpiar.setText("Limpiar");
        guardar.setText("Guardar");
        browse.setText("Escojer Directorio");
        label.setText("  ");
        cuantas.setText("0 Archivos");
        listado.setBackground(Color.WHITE);
        listado.setEditable(false);
        contador = 0;

        guardar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                GuardarComoAccion();
            }

        });

        SubmenuExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fileExit_ActionPerformed(e);
            }
        });

        limpiar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                listado.setText("");
                contador = 0;
                cuantas.setText(contador + " Archivos");
            }
        });

        browse.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                AbrirAccion();
            }
        });

        buscar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                FileFilter filtroMp3 = new FileFilter() {
                    public boolean accept(File file) {
                        return file.getName().toLowerCase().endsWith(".mp3");
                    }
                };
                mp3s = file.listFiles(filtroMp3);
                for (int i = 0; i < mp3s.length; i++) {
                    listado.setText(listado.getText() + mp3s[i].getName() + "\n");
                    contador++;
                    cuantas.setText(contador + " Archivos");
                }
            }
        });

        Submenu.add(SubmenuExit);
        menu.add(Submenu);
        Panel1.add(browse, null);
        Panel1.add(buscar, null);
        Panel1.add(limpiar, null);
        Panel1.add(guardar, null);
        Panel1.add(cuantas, null);
        Panel2.add(label, null);
        jScrollPane1.getViewport().add(listado, null);
        jScrollPane1.setPreferredSize(new Dimension(800, 600));
        this.setJMenuBar(menu);
        this.getContentPane().add(Panel1, BorderLayout.NORTH);
        this.getContentPane().add(Panel2, BorderLayout.SOUTH);
        this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);

    }

    private void fileExit_ActionPerformed(ActionEvent e) {
        System.exit(0);
    }

    private void AbrirAccion() {
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int returnVal = fc.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            file = fc.getSelectedFile();
            label.setText(fc.getSelectedFile().getPath());
        }
    }

    private void GuardarComoAccion() {
        int returnVal = chooser.showSaveDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            String nombreArchivo = chooser.getSelectedFile().getPath();
            try {
                File file = new File(nombreArchivo);
                FileWriter out = new FileWriter(file);
                String text = listado.getText();
                out.write(text);
                out.close();
            } catch (IOException e1) {
                JOptionPane.showMessageDialog(null, "No se Pudo Guardar '" + nombreArchivo + "'", "Error de Escritura", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    public static void main(String args[]) {
        mp3 window = new mp3();
        window.setTitle("LMP3");
        window.pack();
//        window.show();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
    }
}
